package com.example.active.queue;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;
import java.util.Scanner;

public class Sender implements Runnable {

    public String url;
    public String queue;

    public Sender(String url, String queue) {
        this.url = url;
        this.queue = queue;
    }

    @Override
    public void run() {
        try {
            ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
            Connection connection = connectionFactory.createConnection();
            connection.start();
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            Destination destination = session.createQueue(queue);
            MessageProducer producer = session.createProducer(destination);
            Scanner in = new Scanner(System.in);
            while (in.hasNext()) {
                TextMessage message = session.createTextMessage(in.nextLine());
                producer.send(message);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
